
const Num = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"]
let Ten = ["", "十", "百", "千"]
let Wan = ["", "万", "亿", "兆", "京"]


proc toChinese*(digits: Natural): string =
  ## convert `digits` to a chinese digit string
  ##
  runnableExamples:
    assert toChinese(1) == "一"
    assert toChinese(11) == "十一"
  if digits == 0: return Num[0]

  let s = $digits

  var
    i = 0
    j = s.len - 1
    lastDigit = j

  result = ""
  
  if s[0] == '1' and j mod 4 == 1 and j > 0:
    result.add Ten[1]
    i.inc
    j.dec
    lastDigit = j
    
  while i < len(s):
    if s[i] != '0':
      if lastDigit > j.succ: result.add Num[0]

      lastDigit = j
      # 数字和对应的位
      result.add Num[ord(s[i]) - ord('0')]
      result.add Ten[j mod 4]

    # 补上万,亿
    if j mod 4 == 0 and lastDigit < j + 4:
      # 万,亿之前的零会被跳过
      lastDigit = j
      result.add Wan[j div 4]
    j.dec
    i.inc
  return
  